<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------

namespace api\wxapp\controller;

use api\wxapp\controller\ApiBaseController;
use think\Db;
use api\wxapp\model\Coupon;
class CouponController extends ApiBaseController
{

    public function index(Coupon $coupon)
    {
        $params = $this->getParam();
        $this->checkuid($params);
        $where[] = ['t2.uid','eq',$params['uid']];
        //默认查询为使用的优惠券
        !isset($params['type']) ? $params['type'] = 1 : '';
        if($params['type'] == 3){
            //已过期
            $where[] = ['t1.etime','lt',date('Y-m-d H:i:s',time())];
        }else{
            $where[] = ['t2.type','eq',$params['type']];
        }
        $list =  Coupon::alias('t1')
            ->join('app_coupon_list t2','t2.coupon_id = t1.id')
            ->where($where)
            ->all();
        $this->success('请求成功!',$list);
    }
    public function delete()
    {
        $params = $this->getParam();
        $this->checkuid($params);
        $where[] = ['etime','lt',date('Y-m-d H:i:s',time())]; //过期
        $idarr = Coupon::where($where)->column('id');
        //清除已过期的优惠券  
        $map[] = ['coupon_id','in',$idarr];
        $map[] = ['uid','eq',$params['uid']];
        $map[] = ['type','eq',1];
        $res = db::name('app_coupon_list')->where($map)->delete();
        if($res){
            $this->success('清空成功!',true);
        }else{
            $this->error('清空失败!',false);
        }
    }
}
